package scct.hys.dao;

import scct.hys.model.Cinema;
import scct.hys.model.User;
import scct.hys.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CinemaDao {


    public List<Cinema> getAllCinemas(){
        List<Cinema> list=new ArrayList<>();
        Connection conn= DBUtils.getConnection();
        String sql="select * from cm_cinema";
        PreparedStatement pstmt=null;
        try {
            pstmt= conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                String address=rs.getString("address");
                String totalseats=rs.getString("totalseats");
                list.add(new Cinema(id,name,address,totalseats));
            }
            DBUtils.closeConnection(conn);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }
    public static boolean addCinema(String name, String address, String totalseats) {
        System.out.println(name);
        System.out.println(address);
        System.out.println(totalseats);
        int result=0;
        Connection conn=DBUtils.getConnection();
        String insert_sql="insert into cm_cinema values(null,?,?,?)";
        try {
            PreparedStatement pstmt=conn.prepareStatement(insert_sql);
            pstmt.setString(1,name);
            pstmt.setString(2,address);
            pstmt.setString(3,totalseats);
            result=pstmt.executeUpdate();
            DBUtils.closeConnection(conn);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return result>0;
    }
    public boolean updateCinema(int id, String name, String address, String totalseats){
        int count=0;
        Connection conn=DBUtils.getConnection();
        String update_sql="update cm_cinema set name=?,address=?,totalseats=? where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(update_sql);
            pstmt.setString(1,name);
            pstmt.setString(2,address);
            pstmt.setString(3,totalseats);
            pstmt.setInt(4,id);
            count=pstmt.executeUpdate();

        }catch (SQLException e){
            e.printStackTrace();
        }
        return count>0;
    }

    public  Cinema getCinemaById(int id){
        Cinema cinema=null;
        Connection conn=DBUtils.getConnection();
        String sql="select * from cm_cinema where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs=pstmt.executeQuery();
            if (rs.next()){
                String name=rs.getString("name");
                String address=rs.getString("address");
                String totalseats=rs.getString("totalseats");
                cinema=new Cinema(id,name,address,totalseats);
            }
            DBUtils.closeConnection(conn);

        }catch (SQLException e){
            e.printStackTrace();
        }
        return cinema;
    }
    public boolean delCinema(int id) {
        System.out.println("id:"+id);
        int num=0;
        Connection conn=DBUtils.getConnection();
        String del_sql="delete from cm_cinema where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(del_sql);
            pstmt.setInt(1,id);
            num=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return num>0;

    }
    public ArrayList<User> getUserByName(String name) {
        ArrayList<User> users=new ArrayList<>();
        Connection conn=DBUtils.getConnection();

        String sql="select * from cm_user where name like '%"+name +"%'";
        try{
            PreparedStatement pstmt=conn.prepareStatement(sql);

            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String username=rs.getString(2);
                String password=rs.getString("password");
                int age=rs.getInt("age");
                String gender=rs.getString("gender");
                String mobile=rs.getString("mobile");
                users.add(new User(id,username,password,age,gender,mobile));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return  users;


    }
}
